//-----------------Sonic CD Title Card Script-----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0			:	Object.BarPos
#alias Object.Value1			:	Object.TextPos
#alias Object.Value2			:	Object.TextSize
#alias Object.Value3			:	Object.Timer
#alias Object.Value4			:	Object.FadeR
#alias Object.Value5			:	Object.FadeG
#alias Object.Value6			:	Object.FadeB
#alias Object.Value7			:	Object.CharPos

// Player Aliases
#alias Object[0].Value4			:	Player.InvincibleTimer

// HUD Alias
#alias Object[24].PropertyValue	:	HUD.CurrentTimePeriod

// States
#alias 0	:	TITLECARD_FADEIN
#alias 1	:	TITLECARD_SLIDEIN
#alias 2	:	TITLECARD_DISPLAY
#alias 3	:	TITLECARD_SLIDEOUT

// Soundtrack
#alias 0	:	OST_JP

// Players
#alias 0	:	PLAYER_SONIC_A
#alias 1	:	PLAYER_TAILS_A

// Time Period Aliases
#alias 0	:	TIME_PRESENT
#alias 1	:	TIME_PAST
#alias 2	:	TIME_GOOD_FUTURE
#alias 3	:	TIME_BAD_FUTURE


// ControlMode Aliases
#alias -1	:	CONTROLMODE_NONE
#alias 0	:	CONTROLMODE_NORMAL

// Priority
#alias 1	:	PRIORITY_ACTIVE


sub ObjectPlayerInteraction
	Player.Timer = 0
end sub


sub ObjectDraw
	switch Object.State
	case TITLECARD_FADEIN
		Player.ControlMode	= CONTROLMODE_NONE
		Player.Up			= false
		Player.Down			= false
		Player.Left			= false
		Player.Right		= false
		Player.JumpPress	= false
		Player.JumpHold		= false

		// Check if the player comes from warping, if not, blank speed
		if Warp.XPos < 1
			Player.Speed = 0
		end if

		if Object.Timer > 0
			if Object.Timer == 256
				PlayMusic(0)
				if Warp.XPos > 0
					// Restore values post-warp
					Player.XPos = Warp.XPos
					Player.YPos = Warp.YPos
					
					Player.XVelocity = Warp.XVelocity
					Player.YVelocity = Warp.YVelocity
					Player.Speed	 = Warp.XVelocity

					Player.State = Warp.State

					Stage.MilliSeconds = Rec_Milliseconds
					Stage.Seconds	   = Rec_Seconds
					Stage.Minutes	   = Rec_Minutes

					Player.InvincibleTimer = 8000
				end if
				Warp.Timer = 0
				Warp.Frame = 0
			end if

			SetScreenFade(Object.FadeR, Object.FadeG, Object.FadeB, Object.Timer)
			Object.Timer -= 8
		else
			Fade_Colour = 0

			// the title card animation doesn't play out if you come from a warp or restarted from a checkpoint
			CheckEqual(Warp.XPos, 0)
			TempValue0  = CheckResult
			CheckLower(LampPost.Check, 32)
			TempValue0 &= CheckResult
			if TempValue0 == true
				Object.State = TITLECARD_SLIDEIN
				Object.Timer = 0
			else
				Player.ControlMode = CONTROLMODE_NORMAL
				Stage.TimeEnabled  = true

				ResetObjectEntity(20, TypeName[Blank Object], 0, 0, 0)
				Warp.XPos = 0
			end if
		end if
		break

	case TITLECARD_SLIDEIN
		TempValue0  = 144
		TempValue0 += Object.CharPos

		if Object.TextPos > TempValue0
			// Slide the text to the left
			Object.TextPos -= 8
			
			// Now, this part got changed in an update
			
		else
			// -> Updated version - both the text AND the bar have to be on screen for the title card to progress

			if Object.BarPos == 0
				Object.State = TITLECARD_DISPLAY
			end if
		end if
		
		if Object.BarPos < 0
			// Move the Bar down
			Object.BarPos += 8
			
			// There used to be code here in older versions, but not anymore...
			// -> Initial version - only the bar needed to be in the proper position for the title card to continue
			//    This had text get incorrectly often
			
		end if
		break

	case TITLECARD_DISPLAY
		// Hold for a moment to let the Player see what a nice Title Card this is
		
		if Object.Timer == 90
			// Time's up, start sliding out!
			Object.State = TITLECARD_SLIDEOUT
		else
			Object.Timer++
		end if
		break

	case TITLECARD_SLIDEOUT
		if Object.TextPos < 408
		
			// Move the text to the right, and...
			Object.TextPos += 16
			
			// ...move the bar upwards as well
			Object.BarPos -= 16
			
		else
			// Free Sonic's control, and enable the stage timer.
			Player.ControlMode = CONTROLMODE_NORMAL
			Stage.TimeEnabled  = true
			
			// The Title Card's job is done, despawn it
			ResetObjectEntity(20, TypeName[Blank Object], 0, 0, 0)
			Warp.XPos = 0
		end if
		break
		
	end switch
	
	// Before drawing, make sure this object hasn't been blanked already
	// (Under certain circumstances, this can otherwise cause major graphical issues!!)
	if Object.Type > TypeName[Blank Object]
		TempValue0  = 136
		TempValue0 += Object.CharPos
		
		// Draw the Red Bar
		DrawSpriteScreenXY(0, TempValue0, Object.BarPos)

		// And then, draw the act number
		DrawSpriteScreenXY(Stage.ActNo, Object.TextPos, 80)
		
		// And then draw the Zone text, the Sonic CD bar, and the weird three lines
		DrawSpriteScreenXY(4, Object.TextPos, 80)
		DrawSpriteScreenXY(5, Object.TextPos, 80)
		DrawSpriteScreenXY(6, Object.TextPos, 80)
		DrawSpriteScreenXY(7, Object.TextPos, 80)

#platform: Use_Decomp	// Creates a black rectangle that fills the empty space on wider resolutions
		Tempvalue0  = Object.TextPos
		TempValue0 += 220
		DrawRect(TempValue0, 163, Screen.XSize, 9, 0, 0, 0, 255)
#endplatform
		
		// And then Draw the Round Name
		// (Letters start at Sprite Frame 8)
		TempValue0 = 8
		while TempValue0 < Object.TextSize
			DrawSpriteScreenXY(TempValue0, Object.TextPos, 80)
			TempValue0++
		loop
	end if
	
end sub


sub ObjectStartup
	// Load the Display sheet for the current Player
	if Stage.PlayerListPos == PLAYER_SONIC_A // PLAYER_SONIC in origins
		LoadSpriteSheet("Global/Display.gif")
	end if
	if Stage.PlayerListPos == PLAYER_TAILS_A // PLAYER_TAILS in origins
		LoadSpriteSheet("Global/Display_t.gif")
	end if
#platform: Use_Origins
	if Stage.PlayerListPos == PLAYER_KNUCKLES
		LoadSpriteSheet("Global/Display_k.gif")
	end if
	if Stage.PlayerListPos == PLAYER_AMY
		LoadSpriteSheet("Global/Display_a.gif")
	end if
#endplatform
	
	// Set the music for the Stage
	if Options.Soundtrack == OST_JP
		switch HUD.CurrentTimePeriod
		case TIME_PRESENT
			SetMusicTrack("JP/R4A.ogg", 0, 1)
			break
		case TIME_PAST
			SetMusicTrack("R4B.ogg", 0, 1)
			break
		case TIME_GOOD_FUTURE
			SetMusicTrack("JP/R4C.ogg", 0, 1)
			break
		case TIME_BAD_FUTURE
			SetMusicTrack("JP/R4D.ogg", 0, 1)
			break
		end switch
	else
		switch HUD.CurrentTimePeriod
		case TIME_PRESENT
			SetMusicTrack("US/R4A.ogg", 0, 415148)
			break
		case TIME_PAST
			SetMusicTrack("R4B.ogg", 0, 1)
			break
		case TIME_GOOD_FUTURE
			SetMusicTrack("US/R4C.ogg", 0, 1)
			break
		case TIME_BAD_FUTURE
			SetMusicTrack("US/R4D.ogg", 0, 145616)
			break
		end switch
	end if

	Flower_TypeNo   = TypeName[Flower]
	Object[20].Type = TypeName[Title Card]
	
	// Different Fade-In times depending on if this is a new level load or if the Player is warping in
	if Warp.XPos > 0
		// If the Player is warping in, have a shorter fade-in time
		Object[20].Timer = 256
	else
		Object[20].Timer = 384
	end if

	// In the first act of Tidal Tempest, set a Roof to be active
	if Stage.ActNo == 1
		Player.RoofBarrier = true
	end if

	// Make the Title Card always active, and have it draw above everything
	Object[20].Priority  = PRIORITY_ACTIVE
	Object[20].DrawOrder = 6
	
	// Set initial Bar & Text Positions
	Object[20].BarPos = -216
	Object[20].TextPos = 336
	

	Object[20].CharPos  = Screen.CenterX
	Object[20].CharPos -= 160
	
	if Object[20].CharPos > 3
		Object[20].CharPos -= 4
	end if
	
	Object[20].CharPos &= 248

	Object[20].TextPos  += Object[20].CharPos
	Object[20].TextPos  += Object[20].CharPos

	// "Tidal Tempest" is 12 letters long
	Object[20].TextSize  = 14
	
	// Bump the value by 8 to correspond with the order of the Sprite Frames
	Object[20].TextSize += 8
	
	// Preserve the color used from the last fade. Fade_Colour is stored as RRGGBB, in hex
	
	Object[20].FadeR   = Fade_Colour
	Object[20].FadeR >>= 16

	Object[20].FadeG   = Fade_Colour
	Object[20].FadeG  &= 0x00FF00
	Object[20].FadeG >>= 8

	Object[20].FadeB   = Fade_Colour
	Object[20].FadeB  &= 0x0000FF
	
	// 0 - Red Bar Frame
	SpriteFrame(0, 0, 32, 200, 223, 1)
	
	// 1-3 - Act Bubbles
	SpriteFrame(96, 96, 48, 48, 41, 1)
	SpriteFrame(96, 96, 48, 48, 90, 1)
	SpriteFrame(96, 96, 48, 48, 139, 1)
	
	// 4 - "Zone" text
	SpriteFrame(64, 97, 48, 16, 41, 67)
	
	// 5 - Main part of the "SONIC THE HEGDEHOG CD" bar
	
	// Workaround for steam datafile to show this sprite correctly
#platform: Use_Origins
	SpriteFrame(72, 81, 160, 16, 49, 50)	// #5  - Sonic The Hedgehog CD
#endplatform

#platform: Use_Standalone
	#platform: Standard
		SpriteFrame(64, 81, 160, 16, 41, 50)// #5  - Sonic The Hedgehog CD
	#endplatform
#endplatform

#platform: Use_Standalone
	#platform: Mobile
		SpriteFrame(72, 81, 160, 16, 49, 50)// #5  - Sonic The Hedgehog CD
	#endplatform
#endplatform

	// 6 - Continuation of the Sonic CD bar
	SpriteFrame(16, 81, 160, 16, 41, 50)
	
	// 7 - Set of three lines
	SpriteFrame(120, 32, 24, 48, 188, 1)
	
	// 8-21 - Individual letters spelling out Tidal Tempest - Приливная Волна
	//Edited by MilesGlitch
	SpriteFrame(0, 1, 16, 55, 200, 133)     // #8  - T - П
	SpriteFrame(17, 33, 10, 23, 211, 84)   	// #9  - I - р
	SpriteFrame(28, 33, 11, 23, 187, 84)    // #10 - D - и
	SpriteFrame(40, 33, 11, 23, 104, 108)  	// #11 - A - л
	SpriteFrame(52, 33, 11, 23, 187, 84)   	// #12 - L - и
	SpriteFrame(64, 33, 11, 23, 55, 84)     // #13 - T - в
	SpriteFrame(76, 33, 11, 23, 119, 84)    // #14 - E - н
	SpriteFrame(88, 33, 11, 23, 43, 84)    	// #15 - M - а
	SpriteFrame(100, 33, 12, 23, 57, 108)	// #16 - P - я

	SpriteFrame(0, 56, 19, 55, 256, 47)     // #17 - E - В
	SpriteFrame(20, 57, 11, 23, 199, 84)	// #18 - S - о
	SpriteFrame(32, 57, 11, 23, 104, 108)	// #19 - T - л
	SpriteFrame(44, 57, 11, 23, 119, 84)	// #20 - T - н
	SpriteFrame(56, 57, 11, 23, 43, 84)		// #21 - T - а
	
	
#platform: Use_Origins
	if game.playMode == BOOT_PLAYMODE_MISSION
		TempValue0 = true
	else
		if game.playMode == BOOT_PLAYMODE_BOSSRUSH
			TempValue0 = true
		else
			TempValue0 = false
		end if
	end if
	
	if TempValue0 != false
		PlayMusic(0)
		
		game.missionCondition = 0
		game.missionEnd		  = false

		Player.ControlMode	  = CONTROLMODE_NORMAL
		Stage.TimeEnabled	  = true

		ResetObjectEntity(20, TypeName[Blank Object], 0, 0, 0)
		Warp.XPos = 0
	end if
#endplatform
end sub


// ========================
// Editor Subs
// ========================

sub RSDKDraw
	Object.TextSize = 12
	Object.TextSize += 8

	TempValue0 = 136
	TempValue0 += Object.CharPos
	TempValue0 <<= 16

	TempValue1 = 0x500000
	TempValue1 += Object.YPos

	DrawSpriteXY(0, Object.XPos, Object.YPos)
	DrawSpriteXY(Stage.ActNo, Object.XPos, TempValue1)
	DrawSpriteXY(4, Object.XPos, TempValue1)
	DrawSpriteXY(5, Object.XPos, TempValue1)
	DrawSpriteXY(6, Object.XPos, TempValue1)
	DrawSpriteXY(7, Object.XPos, TempValue1)

	TempValue0 = 8
	while TempValue0 < Object.TextSize
		DrawSpriteXY(TempValue0, Object.XPos, TempValue1)
		TempValue0++
	loop
end sub


sub RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	// 0 - Red Bar Frame
	SpriteFrame(0, 0, 32, 200, 223, 1)
	
	// 1-3 - Act Bubbles
	SpriteFrame(96, 96, 48, 48, 41, 1)
	SpriteFrame(96, 96, 48, 48, 90, 1)
	SpriteFrame(96, 96, 48, 48, 139, 1)
	
	// 4 - "Zone" text
	SpriteFrame(64, 97, 48, 16, 41, 67)
	
	// 5 - Main part of the "SONIC THE HEGDEHOG CD" bar
	SpriteFrame(64, 81, 160, 16, 41, 50)
	
	// 6 - Continuation of the Sonic CD bar
	SpriteFrame(16, 81, 160, 16, 41, 50)
	
	// 7 - Set of three lines
	SpriteFrame(100, 32, 24, 48, 188, 1)
	
	// 8-19 - Individual letters spelling out Tidal Tempest - Приливная Волна
	//Edited by MilesGlitch
	SpriteFrame(0, 1, 16, 55, 200, 133)     // #8  - T - П
	SpriteFrame(17, 33, 10, 23, 211, 84)   	// #9  - I - р
	SpriteFrame(27, 33, 11, 23, 187, 84)    // #10 - D - и
	SpriteFrame(38, 33, 11, 23, 104, 108)  	// #11 - A - л
	SpriteFrame(49, 33, 11, 23, 187, 84)   	// #12 - L - и
	SpriteFrame(60, 33, 11, 23, 55, 84)     // #13 - T - в
	SpriteFrame(71, 33, 11, 23, 119, 84)    // #14 - E - н
	SpriteFrame(82, 33, 11, 23, 43, 84)    	// #15 - M - а
	SpriteFrame(93, 33, 12, 23, 57, 108)	// #16 - P - я

	SpriteFrame(0, 56, 19, 55, 256, 47)     // #17 - E - В
	SpriteFrame(20, 57, 11, 23, 199, 84)	// #18 - S - о
	SpriteFrame(31, 57, 11, 23, 104, 108)	// #19 - T - л
	SpriteFrame(41, 57, 11, 23, 119, 84)		// #20 - T - н
	SpriteFrame(0, 57, 11, 23, 43, 84)		// #21 - T - а


	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
